package com.scalar.forecast.systemprefs.actions;

import com.scalar.forecast.actionCommands.ActionCommand;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.forecast.commons.logger.Logger;
import com.scalar.forecast.commons.logger.LoggerManager;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import com.scalar.doctrac.JSPMessages;

import com.scalar.forecast.ForecastExtendedAction;


/**
 * Process the request from the SystemPreferences screen
 *
 * @author  Basha
 * @version 1.0
 */

public class SystemPreferencesAction extends ForecastExtendedAction
{
    private static final Logger LOGGER = LoggerManager.getLogger( SystemPreferencesAction.class );

    public ActionForward executeRequest( ActionMapping mapping,
                                         ActionForm form,
                                         HttpServletRequest request,
                                         HttpServletResponse response ) throws Exception
    {
        ActionForward actionForward = null;
        String action = request.getParameter( REQUEST_ACTION );
        ActionCommand SystemPreferencesCommand = null;
        //JSPMessages   messages    =  new JSPMessages();
        //request.setAttribute( SessionKeys.MESSAGES, messages );

        //System.out.println("Action in UserPreferencesAction is : " + action );

        LOGGER.log( Logger.DEBUG, "Action in UserPreferencesAction is : " + action );

        //Checking If there is any action specified else redirecting to the search page with out processing any further
        if( ( action == null ) || ( action.trim().length() == 0 ) )
        {
            LOGGER.log( Logger.DEBUG, "No Action specified. So returning Null value" );
            //messages.putMessage( Constants.MESSAGE_TYPE_ERROR,  MessageKeys.MESSAGE_INVALID_REQUEST );
        }
        else
        {
            LOGGER.log( Logger.DEBUG, "Retreiving the Action Command" );
            SystemPreferencesCommand = com.scalar.forecast.systemprefs.actioncommands.SystemPreferencesActionCommandFactory.getActionCommand( action );
            if( SystemPreferencesCommand == null )
            {
                LOGGER.log( Logger.DEBUG, "ActionCommand instance could not be retrived. returning Null Value" );
                //messages.putMessage( Constants.MESSAGE_TYPE_INFO,  MessageKeys.MESSAGE_INVALID_REQUEST );
            }
            else
            {
                try
                {
                    actionForward = SystemPreferencesCommand.execute( mapping, form, request, response );
                }
                catch( Exception e )
                {
                    LOGGER.log( Logger.ERROR, "Error while executing the action command", e );
                   // messages.putMessage( Constants.MESSAGE_TYPE_INFO,  MessageKeys.MESSAGE_ERROR_PROCESSING_REQUEST );
                }
            }
        }
        return actionForward;
    }

}
